//package com.nt.train.advice;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.nt.train.exception.APIException;
//import com.nt.train.vo.AjaxResult;
//import com.nt.train.vo.ResultVo;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.MediaType;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//
///**
// *https://blog.csdn.net/qq_42046105/article/details/126079839
// *  supports：判断是否要交给 beforeBodyWrite 方法执行，ture：需要；false：不需要
//    beforeBodyWrite：对 response 进行具体的处理
// * @author hlf
// * @version 1.0
// * @date 2022/9/21 15:39
// */
//@RestControllerAdvice(basePackages = "com.nt.train.controller")
//public class ResponseAdvice implements ResponseBodyAdvice<Object> {
//    @Override
//    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
//        // 如果不需要进行封装的，可以添加一些校验手段，比如添加标记排除的注解
//        return true;
//    }
//
//
//    @Override
//    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
//                                  Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
//                                  ServerHttpResponse response) {
//        // 提供一定的灵活度，如果body已经被包装了，就不进行包装
//        if (body instanceof AjaxResult) {
//            return body;
//        }
//        if (body instanceof ResultVo) {
//            return body;
//        }
//        if (returnType.getGenericParameterType().equals(String.class)) {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                // 将数据包装在ResultVO里后，再转换为json字符串响应给前端
//                return objectMapper.writeValueAsString(AjaxResult.success(body));
//            } catch (JsonProcessingException e) {
//                throw new APIException("返回String类型错误");
//            }
//        }
//        return AjaxResult.success(body);
//    }
//}